package com.xzbd.springbootmybatiesdm.controller;

import com.xzbd.springbootmybatiesdm.common.response.R;
import com.xzbd.springbootmybatiesdm.config.UploadConfig;
import com.xzbd.springbootmybatiesdm.utils.FileUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("upload")
public class UploadController {

    @Autowired
    private UploadConfig uploadConfig;

    @PostMapping("pic")
    public R uploadPic(MultipartFile file) {
        if (file.isEmpty()) {
            return R.error("图片不能为空");
        }
        String fileName = file.getOriginalFilename();
        long fileSize = file.getSize();

		fileName = FileUtil.renameToUUID(fileName);
        String picPath = uploadConfig.getPicPath();
		// FileDO sysFile = new FileDO(FileType.fileType(fileName), "/files/" + fileName, new Date());
		try {
			FileUtil.uploadFile(file.getBytes(), picPath, fileName);
		} catch (Exception e) {
			return R.error("图片上传失败");
		}

		// if (sysFileService.save(sysFile) > 0) {
		// 	return R.ok().put("fileName",sysFile.getUrl());
		// }


      
        log.info("pic is save in {} path", picPath );
        return R.ok("pic is upload");
    }
    @PostMapping("file")
    public R uploadFile(MultipartFile file) {
        if (file.isEmpty()) {
            return R.error("文件不能为空");
        }
        String fileName = file.getOriginalFilename();
        long fileSize = file.getSize();

		fileName = FileUtil.renameToUUID(fileName);
        String docPath = uploadConfig.getDocPath();
		// FileDO sysFile = new FileDO(FileType.fileType(fileName), "/files/" + fileName, new Date());
		try {
			FileUtil.uploadFile(file.getBytes(), docPath, fileName);
		} catch (Exception e) {
			return R.error("文件上传失败");
		}

		// if (sysFileService.save(sysFile) > 0) {
		// 	return R.ok().put("fileName",sysFile.getUrl());
		// }


      
        log.info("file is save in {} path", docPath );
        return R.ok("file is upload");
    }
}